package com.class5.springboot.frame.repo.real;

import com.class5.springboot.frame.repo.tool.xml.DbProjectFactory;
import com.class5.springboot.frame.repo.tool.xml.DbTable;

import java.util.HashMap;
import java.util.Map;

public class EntityMapperFactory {
    // 本地缓存
    private static Map<Class<?>, EntityMapper> cache = new HashMap<Class<?>, EntityMapper>();

    /**
     * 根据实体类获取映射对象
     *
     * @param entityClass
     * @return
     */
    public synchronized static EntityMapper get(Class<?> entityClass) {
        EntityMapper m = cache.get(entityClass);
        if (m != null)
            return m;
        synchronized (cache) {
            DbTable table = DbProjectFactory.getByEntityClass(entityClass);
            if (table == null) {
                cache.put(entityClass, null);
                return null;
            }
            m = new EntityMapper(entityClass, table);
            cache.put(entityClass, m);
            return m;
        }
    }
}
